package com.ghd.dataviz.controller;

import com.ghd.dataviz.core.param.DatavizResponse;
import com.ghd.dataviz.core.security.JwtTokenUtil;
import com.ghd.dataviz.core.utils.JsonUtil;
import com.ghd.dataviz.utils.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author ：ghd
 * @date ：Created in 2020-03-10 22:23
 * @description：刷新令牌 api
 * @modified By：
 * @version: 1.0$
 */
@RestController
@Api(tags = "认证令牌接口")
@RequestMapping("token")
public class TokenController {
    @Resource(name = "userDetailsService")
    private UserDetailsService userDetailsService;

    @PostMapping("/refresh")
    @ApiOperation(value = "刷新令牌", notes = "刷新认证令牌，data为返回的新令牌")
    public DatavizResponse<String> refreshToken() {
        String userName = UserUtil.getUserName();
        UserDetails userDetails = userDetailsService.loadUserByUsername(userName);
        String token = JwtTokenUtil.createToken(JsonUtil.objectToJson(userDetails));
        return DatavizResponse.success("refresh token success!", token);
    }
}
